﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace CYZH.Common
{
    public class ConfigHelper
    {
        private static string ConfigPath= string.Format("{0}.config", Assembly.GetEntryAssembly().Location);

        public static Configuration confg = GetConfig();

        public static string oracleConn()
        {
            return System.Configuration.ConfigurationManager.AppSettings["orclConnStr"]; ;
        }
        public static string mysqlConn()
        {
            return System.Configuration.ConfigurationManager.AppSettings["mysqlConnStr"]; ;
        }
        public static string BASE_PATH()
        {
            return System.Configuration.ConfigurationManager.AppSettings["FileBasePATH"]; 
        }
        //public static double [] AscGeoTrans()
        //{
        //    //Configuration config = GetConfig();

        //    CustomSection appsection = (CustomSection)confg.GetSection("customSetting");
            
        //  //  string strminx = GetAppSettingsByKey("Asc_MinX");
        // //   string strmaxy = GetAppSettingsByKey("Asc_MaxY");
        //  //  string strcell = GetAppSettingsByKey("Asc_CellSize");



        //    double Asc_MinX = Convert.ToDouble(appsection.AscSetting.MinX);
        //    double Asc_MaxY = Convert.ToDouble(appsection.AscSetting.MaxY);
        //    double Asc_CellSize = Convert.ToDouble(appsection.AscSetting.CellSize);
        //    double[] geotrs = { Constants.Asc_MinX, Constants.Asc_CellSize, 0, Constants.Asc_MaxY, 0, -Constants.Asc_CellSize };
        //    return geotrs;
        //}
        //public static double[] GetExtentsByXzqhCode(string code)
        //{
        //    double[] exts = { 0, 0, 0, 0 };
        //    Configuration config = GetConfig();

        //    CustomSection appsection = (CustomSection)config.GetSection("customSetting");

        //    foreach (OutRasterConfigElement rc in appsection.OutRaster)
        //    {
        //        if (rc.Code.Equals(code))
        //        {
        //            string strminx = rc.Extents;
        //            string[] strext = strminx.Split(',');

        //            double MinX = Convert.ToDouble(strext[0]);
        //            double MinY = Convert.ToDouble(strext[1]);
        //            double MaxX = Convert.ToDouble(strext[2]);
        //            double MaxY = Convert.ToDouble(strext[3]);
        //            exts[0] = MinX;// { MinX, MinY, MaxX, MaxY };
        //            exts[1] = MinY;
        //            exts[2] = MaxX;
        //            exts[3] = MaxY;
        //            break;
        //        }
        //    }
        //    return exts;
        //    // string strminx= GetCustomSettingsByKey("152500");
        //    // string strminx = appsection.Item.get;

        //}
        public static OutRasterConfigElement GetConfigParamByXzqhCode(string code)
        {
           
            Configuration config = GetConfig();

            CustomSection appsection = (CustomSection)config.GetSection("customSetting");
            OutRasterConfigElement orc = null;
            foreach (OutRasterConfigElement rc in appsection.OutRaster)
            {
                if (rc.Code.Equals(code))
                {
                    orc = rc;
                    return orc;
                    
                }
            }
            return orc;
            //return exts;
            // string strminx= GetCustomSettingsByKey("152500");
            // string strminx = appsection.Item.get;

        }
        //public static int[] AscColRow()
        //{

        //   // string Asc_NumCols = System.Configuration.ConfigurationManager.AppSettings["Asc_NumCols"];
        //   // string Asc_NumRows = System.Configuration.ConfigurationManager.AppSettings["Asc_NumRows"];
        //    //string Asc_CellSize = System.Configuration.ConfigurationManager.AppSettings["Asc_CellSize"];
        //   // int x = Convert.ToInt32(Asc_NumCols);
        //  //  int y = Convert.ToInt32(Asc_NumRows);
           
        //    int[] geotrs = { Constants.Asc_NumCols, Constants.Asc_NumRows };
        //    return geotrs;
        //}
        public static Configuration GetConfig()
        {
            //获取调用当前正在执行的方法的方法的 Assembly  
            Assembly assembly = Assembly.GetEntryAssembly();//.GetCallingAssembly();
            string path = string.Format("{0}.config", assembly.Location);

            if (File.Exists(path) == false)
            {
                string msg = string.Format("{0}路径下的文件未找到 ", path);
                throw new FileNotFoundException(msg);
            }

            try
            {
                ExeConfigurationFileMap configFile = new ExeConfigurationFileMap();
                configFile.ExeConfigFilename = path;
                Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configFile, ConfigurationUserLevel.None);

                return config;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
    }
}
